package Comunication;


import java.util.HashMap;
import java.util.Map;

public class Protocols {
	static Map<Integer,Integer> msiMap = new HashMap<Integer,Integer> ();
	 Map<Integer,Integer> msiStateMap = new HashMap<Integer,Integer> ();
	 Map<Integer,Integer> mesiMap = new HashMap<Integer,Integer> ();
	 Map<Integer,Integer> mesiStateMap = new HashMap<Integer,Integer> ();
		
	public Protocols(){
		/*
		 * Machine state MSI protocol: operations
		 *     | PrRd(1)   PrWr(2)     BusRd(3)  BusRdX(4) BusUpgr(5) Flush(6)
		 * -------------------------------------------------------------------
		 * M(1)| -(11)     -(12)       Flush(13) Flush(14) x(15)      x(16)
		 * S(2)| -(21)     BusUpgr(22) -(23)     -(24)     x(25)      x(26)
		 * I(3)| BusRd(31) BusRdX(32)  x(33)     x(34)     x(35)      x(36)
		 * 
		 * State x -> 9
		 */
		msiMap.put (11, 0);	msiMap.put (12, 0);	msiMap.put (13, 6);
		msiMap.put (14, 6);	msiMap.put (15, 9);	msiMap.put (16, 9);
		msiMap.put (21, 0);	msiMap.put (22, 5);	msiMap.put (23, 0);
		msiMap.put (24, 0);	msiMap.put (25, 9);	msiMap.put (26, 9);
		msiMap.put (31, 3);	msiMap.put (32, 4);	msiMap.put (33, 9);
		msiMap.put (34, 9);	msiMap.put (35, 9);	msiMap.put (36, 9);
		
		/*
		 * Machine state MSI protocol: states
		 *     | PrRd(1)   PrWr(2)     BusRd(3)  BusRdX(4) BusUpgr(5) Flush(6)
		 * -------------------------------------------------------------------
		 * M(1)| M (11)    M (12)      S (13)    I (14)    x (15)     x (16)
		 * S(2)| S (21)    M (22)      S (23)    I (24)    x (25)     x (26)
		 * I(3)| S (31)    M (32)      x (33)    x (34)    x (35)     x (36)
		 */
		msiStateMap.put (11, 1);	msiStateMap.put (12, 1);	msiStateMap.put (13, 2);
		msiStateMap.put (14, 3);	msiStateMap.put (15, 9);	msiStateMap.put (16, 9);
		msiStateMap.put (21, 2);	msiStateMap.put (22, 1);	msiStateMap.put (23, 2);
		msiStateMap.put (24, 3);	msiStateMap.put (25, 9);	msiStateMap.put (26, 9);
		msiStateMap.put (31, 2);	msiStateMap.put (32, 1);	msiStateMap.put (33, 9);
		msiStateMap.put (34, 9);	msiStateMap.put (35, 9);	msiStateMap.put (36, 9);
		
	}
	
	public int getMsiOperation(int key){
		return msiMap.get(key);
	}
	
	public int getMesiOperation(int key){
		return mesiMap.get(key);
	}
	
	public int getMsiState(int key){
		return msiStateMap.get(key);
	}

	public int getMesiState(int key){
		return mesiStateMap.get(key);
	}
	
	public static int getDMblock(String address, int startIndex) {
		String bin = Integer.toBinaryString(Integer.parseInt(address,16));
		return Integer.parseInt(bin.substring(bin.length()-startIndex, bin.length()-3),2);
	}
	
	
	

	public static void main(String args[]){
		Protocols p = new Protocols();
		System.out.println(p.getMsiOperation(14));
		System.out.println(getDMblock("108",5));
	}
}
